home *** CD-ROM | disk | FTP | other *** search
- unit UHTTPApp;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- HTTPApp;
-
- type
- TUSScriptingBlockEvent = procedure (Sender: TObject; Body: string; var ReplaceText: string) of object;
-
- TUSPageProducer = class(TPageProducer)
- private
- FOnScriptingBlock: TUSScriptingBlockEvent;
- protected
- function DoScriptBlock(aBody: string): string;
- public
- function ContentFromStream(Stream: TStream): string; override;
- published
- property OnScriptingBlock: TUSScriptingBlockEvent read FOnScriptingBlock write FOnScriptingBlock;
- { Event fires when a block delimited by <% %> is found. }
- end;
-
- procedure Register;
-
- implementation
-
- uses
- CopyPrsr;
-
- procedure Register;
- begin
- RegisterComponents('Internet', [TUSPageProducer]);
- end;
-
- { TUSPageProducer }
-
- function TUSPageProducer.ContentFromStream(Stream: TStream): string;
- var
- Parser: TCopyParser;
- OutStream: TStringStream;
- ParamStr, ReplaceStr, TokenStr: string;
- ParamList: TStringList;
- LeadingTagChar: Char;
- TrailingTagChar: Char;
-
- function ExtractScriptBody: string;
- begin
- with Parser do
- begin
- Result := SkipToken(False);
- while Token <> toEOF do
- begin
- Result := Result + SkipToToken('%');
- SkipToken(False);
- if Token = '>' then
- Break
- else
- Result := Result + TokenString;
- end;
- end;
- end;
- begin
- LeadingTagChar := '<';
- TrailingTagChar := '>';
-
- OutStream := TStringStream.Create('');
- try
- Parser := TCopyParser.Create(Stream, OutStream);
- with Parser do
- try
- while True do
- begin
- while not (Token in [toEof, LeadingTagChar]) do
- begin
- CopyTokenToOutput;
- SkipToken(True);
- end;
- if Token = toEOF then Break;
- if Token = LeadingTagChar then
- begin
- case SkipToken(False) of
- '#': { preserve support for original TPageProducer }
- begin
- SkipToken(False);
- TokenStr := TokenString;
- ParamStr := TrimLeft(TrimRight(SkipToToken(TrailingTagChar)));
- ParamList := TStringList.Create;
- try
- ExtractHTTPFields([' '], [' '], PChar(ParamStr), ParamList);
- ReplaceStr := HandleTag(TokenStr, ParamList);
- OutStream.WriteString(ReplaceStr);
- finally
- ParamList.Free;
- end;
- SkipToken(True);
- end;
- '%': { new support for <% %> tokens }
- begin
- ReplaceStr := DoScriptBlock(ExtractScriptBody);
- OutStream.WriteString(ReplaceStr);
- SkipToken(True);
- end;
- else
- begin
- OutStream.WriteString(LeadingTagChar);
- Parser.CopyTokenToOutput;
- Parser.SkipToken(True);
- end;
- end;
- end;
- end;
- finally
- Parser.Free;
- end;
- Result := OutStream.DataString;
- finally
- OutStream.Free;
- end;
- end;
-
- function TUSPageProducer.DoScriptBlock(aBody: string): string;
- begin
- Result := aBody;
- if Assigned(FOnScriptingBlock) then
- FOnScriptingBlock(Self, aBody, Result);
- end;
-
- end.
-
-